import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { resolve } from "path";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import unocss from "unocss/vite";

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [
        react(),
        unocss(),
        createSvgIconsPlugin({
            iconDirs: [resolve(process.cwd(), "src/assets/icons")],
            symbolId: "icon-[dir]-[name]",
        }),
    ],

    server: {
        port: 9529,
        proxy: {
            "/api": {
                target: "https://mock.mengxuegu.com",
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, ""),
            },
        },
    },
    resolve: {
        alias: {
            "@": resolve(__dirname, "src"),
            "#": resolve(__dirname, "types"),
        },
        extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"],
    },
    build: {
        // target: "es2015", // 现代浏览器支持
        chunkSizeWarningLimit: 1500, // 提高块大小警告阈值
        polyfillModulePreload: false, // 禁用传统 polyfill
        cssCodeSplit: true, // CSS 代码分割
        terserOptions: {
            compress: {
                drop_console: true, // 移除 console
                drop_debugger: true, // 移除 debugger
            },
        },
        rollupOptions: {
            output: {
                manualChunks(id) {
                    // 自定义分包策略
                    if (id.includes("node_modules")) {
                        return "vendor";
                    }
                },
            },
        },
    },
});
